﻿using System;
using System.Collections.Generic;
using DocumentGenerator.WordDocuments;
using DocumentGenerator.Common;
using System.Drawing;
using System.Diagnostics;
using System.IO;

namespace DocumentGeneratorTest
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateOOXMLDocument();
            CreateODFDocument();
        }

        private static void CreateODFDocument()
        {
            ////Set the filename
            //var fileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Text.odt";
            //var doc = CreateNewDocument(fileName, fileName);

            ////Finally save the document
            //doc.Save(DocumentType.ODFTextDocument);

            //Process.Start(fileName);
        }

        private static void CreateOOXMLDocument()
        {
            //Set the filename
            var fileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\docnew1.docx";
            var orgFilename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Letter1.dotx";

            var doc = CreateNewDocument(fileName, orgFilename);

            //Finally save the document
            doc.Save(DocumentType.OOXMLTextDocument);

            //And open it in Word
            Process.Start(fileName);
        }

        private static TextDocument CreateNewDocument(string fileName, string orgFilename)
        {
            //Create the document outline.
            //var doc = TextDocument.Create(fileName, true);
            //For appending to an existing file use the Open method.
            //File.Copy(orgFilename, fileName, true);
            TextDocument doc;
            using (var stream = File.OpenRead(orgFilename))
            {
                doc = TextDocument.Create(stream);
            }

            if (doc == null) return null;
            doc.Filename = fileName;
            //var doc = TextDocument.Create(fileName, orgFilename, true);

            doc.ReplaceCustomTag("Releasability", "To anyone");
            doc.ReplaceCustomTag("Classification", "none");
            doc.ReplaceCustomTag("Shield", Image.FromFile(@"C:\Users\Marco\Pictures\shape_icon_mo.jpg"));

            //Create a paragraph with a header
            var p1 = doc.AddNamedParagraph("Paragraph 1", "The text of paragraph 1", 1, "9999");
            p1.SetParagraphType("Topic");

            //Now we create a second pargraph as a sub paragraph of the previous one.
            //As the paragraphs are added sequentialy this paragraph will be directly added after p1 and because the paragraph level is 2
            //it will become a subparagraph.
            var subOfP1 = doc.AddNamedParagraph("Paragraph 1a", "The text of paragraph 1a", 2, "9998");
            subOfP1.SetParagraphType("Topic");

            //Now we add only a header
            var header = doc.AddParagraph("Header 1", string.Empty, 1);

            //Now add a paragraph with only text
            var text = doc.AddParagraph("Text for paragraph 1", 1);

            //You can also change the text of the paragraph.
            text.Text = "Text for paragraph 2";

            //Some basic text styles can also be performed.
            var styledParagraph = doc.AddNamedParagraph("Styled Paragraph", "This text is <b>bold</b> and this is <i>italic</i> and some <u>underlined</u> text and <s>strikethrough</s>.", 1, "9997");
            styledParagraph.SetParagraphType("Topic");
            
            //Tabs can also be set.
            var tabbedParagraph = doc.AddParagraph("Tabbed Paragraph", "This text is tabbed \tTab1 \tTab2 \tTab3.", 1);

            //Lets add a table
            var t = doc.AddTable("Table 1");
            //With two columns
            t.AddColumns(new List<string>() { "col 1", "col 2" });
            //And two rows
            t.AddRow(new List<string>() { "row 1 col 1", "row 1 col 2" });
            t.AddRow(new List<string>() { "row 2 col 1", "row 2 col 2" });

            //Add a numbered list
            var numberedList = doc.AddNumberedList();
            numberedList.AddItem(0, "Numbered Item 1");
            numberedList.AddItem(0, "Numbered Item 2");
            numberedList.AddItem(1, "Numbered Item 2a");
            numberedList.AddItem(2, "Numbered Item 2aa");
            numberedList.AddItem(0, "Numbered Item 3");

            //Add the same list but then as a bullet list
            var bulletList = doc.AddBulletList();
            bulletList.AddItem(0, "Bullet Item 1");
            bulletList.AddItem(0, "Bullet Item 2");
            bulletList.AddItem(1, "Bullet Item 2a");
            bulletList.AddItem(2, "Bullet Item 2aa");
            bulletList.AddItem(0, "Bullet Item 3");

            //Add an image
            string imagePath = Path.Combine(System.Windows.Forms.Application.StartupPath, @"Resources\Green Sea Turtle.jpg");
            var img = Bitmap.FromFile(imagePath);
            var picture1 = doc.AddPicture(img, "The turtle"); 

            //Add an image with filepath
            imagePath = Path.Combine(System.Windows.Forms.Application.StartupPath, @"Resources\Humpback Whale.jpg");
            var picture2 = doc.AddPicture(imagePath, "The whale");

            return doc;
        }
    }
}
